﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace PopShops.Entities
{
    public class DealQuery : Query
    {
        public List<String> IncludeKeywords { get; set; }
        public List<String> ExcludeKeywords { get; set; }
        public List<int> DealTypeID { get; set; }
        public DealSort SortBy { get; set; }
        public bool? Specific { get; set; }
        public DateTime? StartOn { get; set; }
        public DateTime? StartOnMin { get; set; }
        public DateTime? StartOnMax { get; set; }
        public DateTime? EndOn { get; set; }
        public DateTime? EndOnMin { get; set; }
        public DateTime? EndOnMax { get; set; }
        public int? MerchantID { get; set; }
        public int? MerchantTypeID { get; set; }

        public int? PageSize { get; set; }
        public int? PageNumber { get; set; }
        public string SubID { get; set; }

        public DealQuery()
            : base(QueryMethod.Deals, "deals", typeof(SearchResults))
        {
            //Deafault SortBy
            SortBy = DealSort.none;
            IncludeKeywords = new List<string>();
            ExcludeKeywords = new List<string>();
            DealTypeID = new List<int>();

            //Catalog Key
            base.filters.Add("catalog_key", PsContext.Current.CatalogKey);
        }


        public override void GenerateParams()
        {
            string param = "";

            //Keywords
            if (IncludeKeywords.Count > 0)
            {
                foreach (String s in IncludeKeywords)
                    param += HttpUtility.UrlEncode(s) + "+";
                param = param.Substring(0, param.Length - 1);
            }

            if (ExcludeKeywords.Count > 0)
            {
                foreach (String s in ExcludeKeywords)
                    param += " -" + HttpUtility.UrlEncode(s);
            }

            if (!string.IsNullOrEmpty(param))
                base.filters.Add("keywords", param);

            //deal_type_id
            if (DealTypeID.Count > 0)
            {
                param = "";
                foreach (int id in DealTypeID)
                    param += id.ToString() + ",";
                param = param.Substring(0, param.Length - 1);

                base.filters.Add("deal_type_id", param);
            }

            //deal_sort
            if (SortBy != DealSort.none)
                base.filters.Add("deal_sort", SortBy.ToString());

            //specific
            if (Specific.HasValue)
                base.filters.Add("specific", Convert.ToInt32(Specific.Value).ToString());

            //start_on
            if (StartOn.HasValue)
                base.filters.Add("start_on", StartOn.Value.ToString(Constants.DateFormat));

            //end_on
            if (EndOn.HasValue)
                base.filters.Add("end_on", EndOn.Value.ToString(Constants.DateFormat));

            //start_on_min
            if (StartOnMin.HasValue)
                base.filters.Add("start_on_min", StartOnMin.Value.ToString(Constants.DateFormat));

            //start_on_max
            if (StartOnMax.HasValue)
                base.filters.Add("start_on_max", StartOnMax.Value.ToString(Constants.DateFormat));

            //end_on_min
            if (EndOnMin.HasValue)
                base.filters.Add("end_on_min", EndOnMin.Value.ToString(Constants.DateFormat));

            //end_on_max
            if (EndOnMax.HasValue)
                base.filters.Add("end_on_max", EndOnMax.Value.ToString(Constants.DateFormat));

            //merchant_id
            if (MerchantID.HasValue)
                base.filters.Add("merchant_id", MerchantID.Value.ToString());

            //merchant_type_id
            if (MerchantTypeID.HasValue)
                base.filters.Add("merchant_type_id", MerchantTypeID.Value.ToString());

            //deal_limit
            if (PageSize.HasValue)
                base.filters.Add("deal_limit", PageSize.Value.ToString());

            //deal_offset
            if (PageNumber.HasValue)
                base.filters.Add("deal_offset", PageNumber.Value.ToString());

            //url_subid
            if (!string.IsNullOrEmpty(SubID))
                base.filters.Add("url_subid", SubID);
        }
    }
}
